package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.service.ISysDictDataService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 * 
 * @author wgl
 * @date 2021-04-28
 */
@Service
@Transactional
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;

    @Autowired
    private TbBusinessServiceImpl tbBusinessService;

    @Autowired
    private ISysDictDataService iSysDictDataService;

    @Override
    public int add(BusinessTrackVo businessTrackVo) {
        TbBusinessTrackRecord trackRecord=new TbBusinessTrackRecord();
        BeanUtils.copyProperties(businessTrackVo,trackRecord);
        trackRecord.setCreateTime(DateUtils.getNowDate());
        trackRecord.setCreateBy(SecurityUtils.getUsername());
        int row = tbBusinessTrackRecordMapper.insertTbBusinessTrackRecord(trackRecord);

        if(row > 0) {
            TbBusiness business = new TbBusiness();
            BeanUtils.copyProperties(businessTrackVo, business);
            business.setStatus(TbBusiness.StatusType.FOLLOWING.getValue());
            business.setId(businessTrackVo.getBusinessId());
            row = tbBusinessService.updateTbBusiness(business);
        }

        return row;
    }

    @Override
    public List<TbBusinessTrackRecord> selectTbBusinessTrackRecordListByBusinessId(Long id) {
        List<TbBusinessTrackRecord> tbBusinessTrackRecords =  tbBusinessTrackRecordMapper.selectTbBusinessTrackRecordListByBusinessId(id);
        for (TbBusinessTrackRecord tbBusinessTrackRecord : tbBusinessTrackRecords) {
            String[] items = tbBusinessTrackRecord.getKeyItems().split(",");
            for (String item : items) {
                String dictLable = iSysDictDataService.selectDictLabel("communication_point", item);
                tbBusinessTrackRecord.getKeys().add(dictLable);
            }
        }

        return tbBusinessTrackRecords;
    }
}
